//此文件封装一些常用的方法供使用

/*打开窗口url, id, BisShowWait, Sanishow, Itime
 * url表示窗口地址
 * id表示窗口ID
 * BisShowWait是否出现等待雪花
 * Sanishow动画类型
 */
function OpenNewWin(option) {
	var BisShowWait = !option.BisShowWait ? false : true
	var Sanishow = !option.Sanishow ? "slide-in-right" : option.Sanishow;
	var Itime = !option.Itime ? 150 : option.Itime;
	var extra = !option.extra ? {} : option.extra;
	var webview = plus.webview.getWebviewById(option.id);
	if (webview) {
		webview.reload(true);
		plus.webview.hide(webview, "none", 0);	
		plus.webview.show(webview, Sanishow, Itime)
	} else {
		mui.openWindow({
			url: option.url,
			id: option.id,
			show: {
				aniShow: Sanishow, //页面显示动画，默认为”slide-in-right“；
				duration: Itime
			},
			extras: extra,
			waiting: {
				autoShow: BisShowWait, //自动显示等待框，默认为true
			}

		});
	}
}
//出现等待
function showLoading() {
	var odiv = document.createElement("div");
	odiv.setAttribute("class", "loading");
	odiv.innerHTML = "<span class='rschedule'></span><div class='rsalert'>正在加载...</div>";
	mui(".mui-content")[0].appendChild(odiv);
}

function removeLoading() {
	if (mui(".loading")[0]) {
		setTimeout(function() {
			mui(".mui-content")[0].removeChild(mui(".loading")[0]);
		}, 200)
	}
}
//刷新某一个页面
function reloadHtml() {
	for (var index = 0; index < arguments.length; index++) {
		var webview = plus.webview.getWebviewById(arguments[index]);
		if (webview) {
			webview.reload(true);
		}

	}
}
//扩展API加载完毕后调用onPlusReady回调函数 
document.addEventListener("plusready", onPlusReady, false);

function onPlusReady() {
	if (plus.networkinfo.getCurrentType() == plus.networkinfo.CONNECTION_NONE) {
		plus.nativeUI.toast("亲，您网络已断开");
	}
	document.addEventListener("netchange", onNetChange, false);
}
//网络发生变化
function onNetChange() {
	var nt = plus.networkinfo.getCurrentType();
	switch (nt) {
		case plus.networkinfo.CONNECTION_ETHERNET:
		case plus.networkinfo.CONNECTION_WIFI:
			console.log("wifi");
			reloadHistory();
			break;
		case plus.networkinfo.CONNECTION_CELL2G:
		case plus.networkinfo.CONNECTION_CELL3G:
		case plus.networkinfo.CONNECTION_CELL4G:
			reloadHistory();
			console.log("2,3,4G");
			break;
		default:
			noInternet();
			plus.nativeUI.toast("亲，您网络已断开");
			break;
	}
}
//弹出菜单
function CMeau(meau) {
	//alert(window.getComputedStyle(meau).display);
	//	var meau = document.getElementById("header_meau");
	if (window.getComputedStyle(meau).display == "none") {
		meau.style.display = "block";
	} else {
		meau.style.display = "none";
	}
}
//断网时候出现加载的事件
function noInternet() {
	var noInternet = document.getElementsByClassName("noInternet")[0];
	if (!noInternet) {
		var odiv = document.createElement("div");
		odiv.setAttribute("class", "noInternet");
		odiv.innerHTML = '<div class="mui-icon mui-icon-paperplane noInternet-icon"></div><div class = "noInternet-no">无网络 </div><div class = "noInternet-text">请检查您的手机是否联网， 点击按钮重新加载 </div> <button class = "noInternet-button" id="noInternet-button">重新加载 </button>';
		mui(".mui-content")[0].appendChild(odiv);
		butt = document.getElementById("noInternet-button");
		butt.addEventListener("click", noInternetButton, false);
	}
}
//断网时的按钮事件
function noInternetButton() {
	if (plus.networkinfo.getCurrentType() == plus.networkinfo.CONNECTION_NONE) {
		return;
	} else {
		var allweb = plus.webview.all();
		var self = plus.webview.currentWebview();
		if (self.id == allweb[allweb.length - 1].id) {
			window.location.reload();
		}
	}
}
//网路连接后重新加载页面
function reloadHistory() {
	var allweb = plus.webview.all();
	var self = plus.webview.currentWebview();
	if (self.id != allweb[allweb.length - 1].id) {
		window.location.reload();
	}
}
//打开订单详情
function openOrderDetail() {
	OpenNewWin({
		url: "orderDetail.html",
		id: "orderDetail.html"
	})
}
//打开支付页
function openOrderPay() {
	OpenNewWin({
		url: "orderPay.html",
		id: "orderPay.html"
	})
}
//打开评价列表
function openOrderJudge() {
	OpenNewWin({
		url: "orderManagerJudgeList.html",
		id: "orderManagerJudgeList.html"
	})
}
//打开支付成功页面
function openOrderPaySuccess() {
	OpenNewWin({
		url: "orderPaySuccess.html",
		id: "orderPaySuccess.html"
	})
}
//刷新订单页面
function refreshOrderManager() {
	reloadHtml("orderManagerAll.html", "orderManagerWaitPay.html", "orderManagerWaitGood.html", "orderManagerWaitJudge.html")
}
//获得页面的可视区域
function getViewWH() {
	var winWidth = document.documentElement.clientWidth || window.innerWidth;
	var winHeight = document.documentElement.clientHeight || window.innerHeight;
	return {
		winW: winWidth,
		winH: winHeight
	}
}
//获得登录状态
function getlogin(urls) {
	if (localStorage.account) {
		return true;
	} else {
		OpenNewWin({
			url: urls,
			id: "loginHead.html"
		});
		return false;
	}
}
//打开圈子详情
function openGuanzhu(option) {
	localStorage.guideId = writeData(option.uid);
	OpenNewWin({
		url: option.urls,
		id: "guanzhu.html"
	});
}
//打开商品详情
function openProductDetail(option) {
	localStorage.goodsId = writeData(option.goodId);
	localStorage.guideId = writeData(option.guideId);
	localStorage.circleName = writeData(option.circleName);
	localStorage.groupId = writeData(option.groupId);
	OpenNewWin({
		url: option.urls,
		id: "productDetail.html"
	})
}
//打开圈子
function openCircle(option) {
	localStorage.guideId = writeData(option.guideId);
	localStorage.circleName = writeData(option.circleName);
	localStorage.groupId = writeData(option.groupId);
	localStorage.circleId = writeData(option.circleId);
	OpenNewWin({
		url: option.urls,
		id: "circleMain.html"
	});

}
//图片预加载
function proloadImg(obj) {
	var imgs = new Image();
	var srcs = obj.getAttribute("data-echo");
	imgs.onload = function() {
		obj.src = srcs;
		obj.classList.add("img-lazyload");
		obj.removeAttribute("data-echo");
	}
	if (srcs) {
		imgs.src = srcs;
	}

}